<?php

namespace App\Models\Dynamic;

use App\Models\BasicModel;
use App\Models\User;
use App\Models\UserFocus;
use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Lists extends BasicModel
{
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'dynamic_lists';


    /**
     * @param $shop_id
     * @param int $type
     * @return array
     */
    public static function getList($uid)
    {

        $friendIds = UserFocus::where('user_id', $uid)->pluck('focus_id');

        // 获取互相关注的用户列表
        $mutualFriendIds = UserFocus::whereIn('user_id', $friendIds)
            ->where('focus_id', $uid)
            ->pluck('user_id');
        $list = self::where([])
            ->orWhere(function ($query) use ($uid, $friendIds) {
                $query->where('visible_range', '1')
                    ->where('status', 1);})
            ->orWhere(function ($query) use ($uid, $mutualFriendIds) {
                $query->where('visible_range', '2')
                    ->whereIn('user_id', $mutualFriendIds);
            })
            ->orWhere(function ($query) use ($uid) {
                $query->where('visible_range', '3')
                    ->where('user_id', $uid);
            })
            ->with(['user'=>function($query){
                $query->select(['id','name','avatar','authentication']);
            }])
            ->orderByDesc('id')
            ->paginate(10);

        $result=[];
        foreach ($list as $item){
            $item->user->is_focus=User::getFocusStatus($uid,$item->user_id);
            $item->tages= Tag::whereIn('id',explode(',',$item->tag_ids))->pluck('name');
            $item->comment_num=Review::where(['dynamic_id'=>$item->id,'review_id'=>0])->count();
            $item->is_like=Likes::where(['user_id'=>$uid,'post_id'=>$item->id,'type'=>1])->first() ? 1 :0;
            $item->is_collect=Likes::where(['user_id'=>$uid,'post_id'=>$item->id,'type'=>2])->first() ? 1 :0;
            $result[] = $item;

        }
        return  $result;
    }

    public static function getTheirList($uid,$their_id)
    {
        $list = self::query()->with(['user'=>function($query){
            $query->select(['id','name','avatar','authentication']);
        }])->where(['user_id'=>$their_id,'status'=>1])->whereIn('visible_range',[1,2])->orderByDesc('id')->paginate(10);
        $result['total']=$list->total();
        foreach ($list as $item){
            if($item->visible_range==2){
                $r1=UserFocus::where(['user_id'=>$uid,'focus_id'=>$item->user_id])->first();
                $r2=UserFocus::where(['user_id'=>$item->user_id,'focus_id'=>$uid])->first();
                if(!$r1 || !$r2) {
                    continue;
                }
            }
            $item->tages= Tag::whereIn('id',explode(',',$item->tag_ids))->pluck('name');
            $result['data'][] = $item;
            $item->comment_num=Review::where(['dynamic_id'=>$item->id])->count();
            $item->is_like=Likes::where(['user_id'=>$uid,'post_id'=>$item->id,'type'=>1])->first() ? 1 :0;
            $item->is_collect=Likes::where(['user_id'=>$uid,'post_id'=>$item->id,'type'=>2])->first() ? 1 :0;
        }
        return  $result;
    }

    public function collectList($ids,$uid){
        $list = self::query()->with(['user'=>function($query){
            $query->select(['id','name','avatar','authentication']);
        }])->where(['status'=>1])->whereIn('id',$ids)->orderByDesc('id')->paginate(10);
        $result=[];
        foreach ($list as $item){
            $item->user->is_focus=User::getFocusStatus($uid,$item->user_id);
            $item->tages= Tag::whereIn('id',explode(',',$item->tag_ids))->pluck('name');
            $item->comment_num=Review::where(['dynamic_id'=>$item->id,'review_id'=>0])->count();
            $item->is_like=Likes::where(['user_id'=>$uid,'post_id'=>$item->id,'type'=>1])->first() ? 1 :0;
            $item->is_collect=Likes::where(['user_id'=>$uid,'post_id'=>$item->id,'type'=>2])->first() ? 1 :0;
            $result[] = $item;

        }
        return $result;
    }
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function tag()
    {
        return $this->hasOne(Tag::class, 'id', 'tag_ids');
    }
}
